package com.xuecheng.content.controller;


import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.api.media.dto.TeachplanlMediaDTO;
import com.xuecheng.api.media.vo.BindTeachplanMediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author xuan
 */
@Slf4j
@RestController
//@RequestMapping("courseBase")
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private CoursePubService coursePubService;

    //分页查询
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,@RequestBody QueryCourseModel model) {
        Long aLong = SecurityUtil.getCompanyId();
        PageVO courseBases = courseBaseService.queryCourseList(params, model, aLong);
        return courseBases;
    }


    //添加课程基本数据
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //把数据转换成dto类型
        CourseBaseDTO courseBase = CourseBaseConvert.INSTANCE.voEntity2(courseBaseVO);
        //通过工具类获取公司和用户信息
        Long companyId = SecurityUtil.getCompanyId();
        //设置公司和登录用户信息
        courseBase.setCompanyId(companyId);
        return courseBaseService.createCourseBase(courseBase);
    }

    //查询回显
    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO byCourseId = courseBaseService.getByCourseId(courseBaseId, companyId);
        return byCourseId;
    }

    //修改基本数据
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.voEntity2(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
       courseBaseDTO.setCompanyId(companyId);

        return courseBaseService.modifyCourseBase(courseBaseDTO);
    }



    //根据id删除基本数据
    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId,companyId);

    }

    //提交课程信息
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId,companyId);

    }

    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId,@PathVariable Long companyId) {

        Map<String, Object> dataMap = courseBaseService.previewCourse(courseId, companyId);
        //使用试图和数据模型对象
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);

        return modelAndView;
    }

    @PostMapping("course_pub/publish/{courseId}")
    public void publish(@PathVariable Long courseId) {
        Long companyId = SecurityUtil.getCompanyId();
        coursePubService.publish(courseId,companyId);
    }


}
